package avicit.bdp.dcs.semistructure.service;

import avicit.bdp.common.base.BaseService;
import avicit.bdp.common.utils.BdpLogUtil;
import avicit.bdp.common.utils.CheckParaUtils;
import avicit.bdp.core.constant.Constants;
import avicit.bdp.dcs.semistructure.dao.AsyncFileDao;
import avicit.bdp.dcs.semistructure.dto.AsyncFileDTO;
import avicit.platform6.core.rest.msg.QueryRespBean;
import com.github.pagehelper.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 异步删除文件数据
 */
@Service
@Transactional
public class AsyncFileService extends BaseService<AsyncFileDao, AsyncFileDTO> {

    private static final Logger logger = LoggerFactory.getLogger(AsyncFileService.class);

    @Autowired
    private AsyncFileDao asyncFileDao;
    @Autowired
    private DcsFileService fileService;
    @Autowired
    private JobFileService jobFileService;

    public QueryRespBean<AsyncFileDTO> getFileList(Integer status, String filePath, String fileName, Integer currentPage, Integer pageSize) throws Exception {
        QueryRespBean<AsyncFileDTO> queryRespBean = new QueryRespBean<>();
        com.github.pagehelper.PageHelper.startPage(currentPage, pageSize);

        Page<AsyncFileDTO> fileList = asyncFileDao.getFileList(status, filePath, fileName);
        long remainNum = 0L;
        float progress = 0F;
        String file_path = "";
        if (CollectionUtils.isNotEmpty(fileList)) {
            for (AsyncFileDTO asyncFileDTO : fileList) {
                file_path = asyncFileDTO.getFilePath();
                if (StringUtils.isNotEmpty(file_path)) {
                    asyncFileDTO.setFileName(file_path.substring(file_path.lastIndexOf("/") + 1));
                }

                if(asyncFileDTO.getTotalRecords() == null || asyncFileDTO.getTotalRecords() <= 0){
                    asyncFileDTO.setDeleteProgress("100%");
                }else{
                    remainNum = jobFileService.countDeviceData(asyncFileDTO.getFilePath());
                    progress = (asyncFileDTO.getTotalRecords() - remainNum) / asyncFileDTO.getTotalRecords();
                    asyncFileDTO.setDeleteProgress(Math.round(progress * 100) + "%");
                }
            }
        }

        queryRespBean.setResult(fileList);
        BdpLogUtil.log4Query(queryRespBean);
        return queryRespBean;
    }

    public Boolean deleteFileData(String fileIds) throws Exception {
        CheckParaUtils.checkStringValidity(fileIds, logger, "没有传入待删除的fileId");
        String[] idArr = fileIds.split(Constants.COMMA);
        for(String fileId : idArr){
            AsyncFileDTO asyncFileDTO = this.selectByPrimaryKey(fileId);
            if(asyncFileDTO != null){
                fileService.asyncDeleteFileData(asyncFileDTO.getFilePath());
            }
        }
        return true;
    }

}
